
const DB = require('../config/Sequelize')
const BackCode = require("../utils/BackCode");
const CodeEnum = require('../utils/CodeEnum')
const RandomTool = require('../utils/RandomTool')
const SecretTool = require('../utils/SecretTool')
const GetUserInfoTool = require('../utils/GetUserInfoTool')
const OrderService = {
    query_pay: async (req) => {
        const { id } = req.query
        if (!id) return BackCode.buildError({ msg: '缺少必要的参数' })
        const token = req.headers.authorization.split(' ').pop()
        const userInfo = SecretTool.jwtVerify(token)
        let orderList = await DB.ProductOrder.findAll({
            where: { product_id: id, account_id: userInfo.id, order_state: 'PAY' },
            raw: true
        })
        if (orderList.length > 0) {
            return BackCode.buildSuccess()
        } else {
            return BackCode.buildResult(CodeEnum.PRODUCT_NO_PAY)
        }
    },
    latest:async(req) => {
        const {id} = req.query
        const list = await DB.ProductOrder.findAll({
            where:{product_id:id},
            order:[['gmt_create','DESC']],
            limit:5
        })
        return BackCode.buildSuccessAndData({data:list})
    }
}
module.exports = OrderService